package com.mcf.xiaolinju.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcf.xiaolinju.vo.phoneReturn;
import com.mcf.xiaolinju.entity.Phone;
import com.mcf.xiaolinju.mapper.PhoneMapper;
import com.mcf.xiaolinju.service.IPhoneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mcf
 * @since 2021-11-23
 */
@Service
public class PhoneServiceImpl extends ServiceImpl<PhoneMapper, Phone> implements IPhoneService {
    @Resource
    PhoneMapper phoneMapper;
    //模糊查询
    public Page<phoneReturn> selectPhonedim(IPage<phoneReturn> page,int id,String name,String work,String phonenum){
        return  phoneMapper.selectPhonedim(page,id,name,work,phonenum);
    }
    //分页查询
    public Page<phoneReturn> selectPhone(IPage<phoneReturn> page, int id){
        return  phoneMapper.selectPhone(page,id);
    }
    //修改
    public String updatePhone(int id,String phone,String name,String work,int updateid){
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if(phoneMapper.updateById(id,phone,name,work,updateid,ldt)>0){
            return "修改成功";
        }
        return "修改失败";
    }
    //删除
    public String del(int id,int updateid){
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if(phoneMapper.del(id,updateid,ldt)>0){
            return "删除成功";
        }
        return "删除失败";
    }
    //批量删除
    @Transactional(rollbackFor =  Exception.class) //回滚
    public String delmany( Integer[] id,Integer updateid){
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if(phoneMapper.delmany(id,updateid,ldt)!=id.length){
           TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "删除失败";
        }
        else{
            return "删除成功";
        }
    }
    //小程序查询全部电话号
    public List<Phone> selectapi(int villageid,String workname) {
        return  phoneMapper.selectapi(villageid,workname);
    }

}
